package com.example.adopt.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adopt.entity.Pet;
import com.example.adopt.entity.Pettype;
import com.example.adopt.service.PetService;
import com.example.adopt.service.PettypeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjl
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    @Resource
    ObjectMapper objectMapper;
    @Autowired
    PetService petService;
    @Autowired
    PettypeService pettypeService;

    @RequestMapping("/pets")
    public String selectAllpets(String petname,String pettypename,
                                Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Pet> petWrapper = new QueryWrapper<>();
        if(!StrUtil.isBlank(petname)){
            petWrapper.like("petname", petname);
        }
        if(!StrUtil.isBlank(pettypename)){
            petWrapper .like("pettypename", pettypename);
        }
        Page<Pet> petPage = new Page<>(page, size);
        IPage<Pet> petIPage = petService.page(petPage,petWrapper);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(petIPage);
        return s;
    }
    @RequestMapping("/pettype")
    public String getPetType() throws JsonProcessingException {
        List<Pettype> pettype = pettypeService.list();
        String s = objectMapper.writeValueAsString(pettype);
        return s;
    }
    @PostMapping("/addPet")
    public String addPet(@RequestBody Pet pet){
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        pet.setPetid(s);
        QueryWrapper<Pet> wrapper = new QueryWrapper<>();
        QueryWrapper<Pet> wrapper1 = wrapper.eq("petname",pet.getPetname());
        Pet one = petService.getOne(wrapper1);
        if(one != null){
            return "添加重复";
        }
        boolean save = petService.save(pet);
        String result = save == true ? "success" : "false";
        return result;
    }
    @DeleteMapping("/delPet")
    public String delPet(String petid){
        boolean remove = petService.removeById(petid);
        String result = remove == true ? "success" : "false";
        return result;
    }
    @PutMapping("/updatePet")
    public String updatePet(@RequestBody Pet pet){
        boolean save = petService.updateById(pet);
        String result = save == true ? "success" : "false";
        return result;
    }
    @PostMapping("/addPetType")
    public String addPetType(@RequestBody Pettype pettype){
        QueryWrapper<Pettype> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Pettype> queryWrapper1 = queryWrapper.eq("pettypename", pettype.getPettypename());
        Pettype one = pettypeService.getOne(queryWrapper1);
        if(one != null ){
            return "该类型已存在，无需添加";
        }
        boolean save = pettypeService.save(pettype);
        String result = save == true ? "success" : "false";
        return result;
    }

}

